import { Suspense } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
import { BrowserRouter } from 'react-router-dom'
let root: any = null;
function render(props: any = {}) {
  const { container } = props;
  const dom = container ? container.querySelector('#root') : document.getElementById('root');

  root = createRoot(dom!);
  root.render(
    <Suspense>
      <BrowserRouter basename={(window as any).__POWERED_BY_QIANKUN__ ? '/team' : '/'}>
        <App />
      </BrowserRouter>
    </Suspense>
  );
}
renderWithQiankun({
  mount(props) {
    console.log('[child-react-app2] mount', props);
    render(props);
  },
  bootstrap() {
    console.log('[child-react-app2] bootstrap');
  },
  unmount(props) {
    console.log('[child-react-app2] unmount', props);
    if (root) {
      root.unmount();
      root = null;
    }
  },
  update(props) {
    console.log('[child-react-app2] update', props);
  },
});

// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render({});
}
